//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Payton Byrd">
//     Copyright (c) 2007 Payton Byrd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

using CBM_Commander.Common.Encoding;

namespace CBM_Commander.DiskImageSharp
{
    public struct BamFields
    {
        public byte[] DiskDosVersionType;
        public byte[] BamRange;
        public byte[] DiskName;
        public byte DiskNamePadding;
        public byte[] DiskID;
        public byte[] DosType;
        public byte[] DoubleSidedFlag;
        public byte DoubleSidedIndicator;
        public byte[] SideTwoFreeSectors;
        public FillRange[] fillRanges;        
    }

    public struct FillRange
    {
        public byte Start;
        public byte Stop;
        public byte Value;
    }

    /// <summary>
    /// Represents a specific sector on the disk.
    /// </summary>
    public class Block
    {
        public ushort Track;
        public ushort Sector;
        public byte[] Data = null;

        public Block(ushort track, ushort sector)
        {
            Track = track;
            Sector = sector;
            Data = new byte[256];
        }
    }

    public class BamBlock : Block
    {
        ImageType type = null;
        public BamBlock(ushort track, ushort sector, ImageType type)
            : base(track, sector)
        {
            this.type = type;

            // Populate the BAM block's default fill ranges;
            foreach (FillRange range in type.BamFields.fillRanges)
            {
                for (byte i = range.Start; i <= range.Stop; i++)
                {
                    Data[i] = range.Value;
                }
            }

            //byte[] DiskDosVersionTypeBytes = 
            //    Petscii.Convert(
        }
    }

    public class DirBlock : Block
    {
        ImageType type = null;
        List<DirEntry> entries = null;

        public DirBlock(ushort track, ushort sector, ImageType type)
            : base(track, sector)
        {
            this.type = type;
            entries = new List<DirEntry>();
        }
    }

    public class DirEntry
    {
    }

    public class FileType 
    {
        private byte value;

        public const byte DEL = 0;
        public const byte SEQ = 1;
        public const byte PRG = 2;
        public const byte USR = 3;
        public const byte REL = 4;
        public const byte CBM = 5;
        public const byte DIR = 6;

        public static implicit operator byte(FileType type)
        {
            return type.value;
        }

        public static implicit operator FileType(byte value)
        {
            FileType type = new FileType();
            type.value = value;
            return type;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is FileType)) return false;
            FileType target = (FileType)obj;
            return value.Equals(target.value);
        }

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override string ToString()
        {
            return value.ToString();
        }
    }
}
